/*
struct ListNode {
	int val;
	struct ListNode *next;
	ListNode(int x) :
			val(x), next(NULL) {
	}
};*/
class Solution {
public:
	ListNode* FindFirstCommonNode(ListNode* pHead1, ListNode* pHead2) {
		int n1 = 0;
		int n2 = 0;
		typedef ListNode Node;
		Node* cur1 = pHead1;
		while (cur1)
		{
			n1++;
			cur1 = cur1->next;
		}
		Node* cur2 = pHead2;
		while (cur2)
		{
			n2++;
			cur2 = cur2->next;
		}
		cout << n1 << endl;
		cout << n2 << endl;

		while (pHead1 && pHead2)
		{
			if (n1 > n2)
			{
				pHead1 = pHead1->next;
				n1--;
			}
			else if (n1 == n2)
			{
				if (pHead1->val == pHead2->val)
				{
					return pHead1;
				}
				pHead1 = pHead1->next;
				pHead2 = pHead2->next;
				n1--;
				n2--;
			}
			else
			{
				pHead2 = pHead2->next;
				n2--;
			}
		}
		return nullptr;
	}
};
